#Requires AutoHotkey v2.0

; 鼠标点击    多屏幕缩放比例必须一致 或者 窗口须在主屏上
clickInSearch(mc*) {
  CoordMode 'mouse', 'Client'       
  for i in mc {
    if mod(A_Index,2) {
      x := i
    } else {
      x ? Click(x,i) : Sleep(i)   ;鼠标点击坐标x为假值时,y表示是要延迟的时间.
    }
  }
}


; 函数框架
activateDocument(&winTitle,&winId,word,foo,mc*) {
  try{
    WinActivate winId
  }catch{
    winId := WinExist(winTitle)
    if winId {
      activateDocument(&winTitle,&winId,word,foo,mc*)
      return
    }else {
      foo()
      winId := WinExist(winTitle)
    }
  }else{
    WinWaitActive winId
    clickInSearch(mc*)
    Sleep 100
    SendText(word)
  }finally{
    CoordMode 'mouse', 'Screen'
    MouseMove(1736, 256, 0)
  }
}

; 绑定临时搜索窗口
bindSearchWindow(content) {
  static BindSearchWinId := 0
  static BindSearchWinTitle
  static mouseClickArr 
  foo(){
    WinWait "A"
    mouseClickArr := getMousePos()
  }
  if (!BindSearchWinId or content = "reset")  {
    BindSearchWinTitle := WinGetTitle("A") 
    BindSearchWinId := WinExist(BindSearchWinTitle)
    foo()
  } 
  activateDocument(&BindSearchWinTitle,&BindSearchWinId,content,foo,mouseClickArr*)
}


; 百度翻译
baiduTranslate(text) {
  winTitle := "百度翻译-您的超级翻译伙伴"
  winId := WinExist(winTitle)
  mouseClickArr := Array(849,390,849,390,353,419)
  
  foo(){
    if RegExMatch(text,"^w") {
      newWindowBrowserRun("https://fanyi.baidu.com/mtpe-individual/multimodal?query=" . OnlyInput.urlEncode(text) . "&lang=en2zh")
    } else {
      newWindowBrowserRun("https://fanyi.baidu.com/mtpe-individual/multimodal?query=" . OnlyInput.urlEncode(text) . "&lang=zh2en")
    } 
    WinWait winTitle
    ; mouseClickArr := getMousePos()
  }
  activateDocument(&winTitle,&winId,text,foo,mouseClickArr*)
}

; ahk文档
ahkDocument(word?) {
  winTitle := " | AutoHotkey v2"
  winId := WinExist(winTitle)
  mouseClickArr := [249,142,263,183,263,183]
  foo() {
    newWindowBrowserRun("https://wyagd001.github.io/v2/docs/index.htm")
    if KeyWait("space", "D T5") {
      clickInSearch(mouseClickArr*)
      Sleep 100
      SendText(word)
    }
  }
  
  activateDocument(&winTitle,&winId,word,foo,mouseClickArr*)
}

;谷歌翻译
googleTranslate(word) {
  winTitle :="Google Translate"
  winId := WinExist(winTitle)
  mouseClickArr := Array(912,376)
  foo() {
    newWindowBrowserRun("https://translate.google.com/?text=" . OnlyInput.urlEncode(word))
    WinWait winTitle
  }
  activateDocument(&winTitle,&winId,word,foo,mouseClickArr*)
}

;谷歌地图
googleEarth(word) {
  winTitle := "Google 地球"
  winId := WinExist(winTitle)
  mouseClickArr := [0,200,414,168,0,200,414,168,0,200,492,171,0,200,414,168]
  foo() {
    newWindowBrowserRun("https://earth.google.com")
    if KeyWait("space", "D T15") {
      clickInSearch(1628,142,0,100,mouseClickArr*)
      Sleep 100
      SendText(word)
    }
  }
  activateDocument(&winTitle,&winId,word,foo,mouseClickArr*)
}

; 小红书
xiaoHongShuSearch(word) {
  winTitle := "小红书搜索"
  winId := WinExist(winTitle)
  mouseClickArr := A_ScreenWidth > A_ScreenHeight ? Array(1172,143,1170,135) : Array(794,149,708,158)
  foo(){
    newWindowBrowserRun("https://www.xiaohongshu.com/explore?channel_id=homefeed_recommend")
    if KeyWait("space", "D T10") {
      clickInSearch(1172,143,1172,143)
      Sleep 100 
      SendText(word)
      Send "{Enter}{Enter}"
    } 
  }
  activateDocument(&winTitle,&winId,word,foo,mouseClickArr*)
}